package com.tchepannou.rails.core.api;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * Implementation of the <a href="http://en.wikipedia.org/wiki/Active_record_pattern">ActiveRecord</a> design pattern.
 *
 * @author  Herve Tchepannou
 */
public interface ActiveRecord
    extends Serializable
{
    //-- Abstract methods
    public abstract long getId ();

    public abstract void setId (long id);



    //-- Persistence methods
    /**
     * Returns all the properties of the ActiveRecord in a <code>HashMap</code>
     */
    public Map toProperties ();

    /**
     * Populate the model properties from a set of name/value pairs
     *
     * @param properties    name/value pairs containing the model properties
     */
    public void populate (Map properties);

    /**
     * Save the model into the database.
     * if the model is new, it get created into the database
     *
     * @param performValidation if <code>true</code>, the model will be validated
     *
     * @return if <code>performValidation</code> is <code>true</code>, this method returns <code>false</code> if the validation fails,
     *         else it will always returns <code>true</code>.
     */
    public boolean save (boolean performValidation);

    /**
     * Delete the model from the database
     */
    public boolean delete (boolean checkIfCanDelete);



    //-- Validation methods
    /**
     * Returns <code>true</code> if the record can be deleted
     */
    public boolean canDelete ();

    /**
     * Returns <code>true</code> is there is a validation saved
     */
    public boolean canSave ();

    /**
     * Returns the list of validation errors
     */
    public  List<String> getErrors ();
}
